﻿$(function () {
    var url = '/manual/invoiceData';

    
    //添加一条发票信息
    $("#saveBtn").click(function (e) {
        

        if (!$("form").valid()) return;
        var trs = $("#dt tr");
        if (trs.length == 0) {
            layer.msg("你不能没有一条明细");
            return;
        }
        layer.confirm('确认提交吗?', {icon: 3, title:'提示'}, function(index){
            //do something
            var companyid = $("#companyid").val();
            var khmc = $("#khmc").val();
            var khsh = $("#khsh").val();
            var dzdh = $("#dzdh").val();
            var dh = $("#dh").val();
            var khyh = $("#khyh").val();
            var yhzh = $("#yhzh").val();
            var bz = $("#bz").val();

            var skr = $("#skr").val();
            var fhr = $("#fhr").val();
            var kpr = $("#kpr").val();

            var spmc = $("input[name='spmc']");
            var spbm = $("input[name='spbm']");
            var productid = $("input[name='productid']");
            var ggxh = $("input[name='ggxh']");
            var jldw = $("input[name='jldw']");
            var sl = $("input[name='sl']");
            var dj = $("input[name='dj']");
            var je = $("input[name='je']");
            var slv = $("select[name='slv'],input[name='slv']");
            var se = $("input[name='se']");

            var sp_arr = new Array();
            var gg_arr = new Array();
            var jldw_arr = new Array();
            var sl_arr = new Array();
            var dj_arr = new Array();
            var je_arr = new Array();
            var slv_arr = new Array();
            var se_arr = new Array();
            var bm_arr = new Array();
            var pid_arr = new Array();


            $.each(spmc, function (index, item) {
                sp_arr.push($(item).val());
                gg_arr.push($(ggxh[index]).val());
                jldw_arr.push($(jldw[index]).val());
                sl_arr.push($(sl[index]).val());
                dj_arr.push($(dj[index]).val());
                je_arr.push($(je[index]).val());
                slv_arr.push($(slv[index]).val());
                se_arr.push($(se[index]).val());
                bm_arr.push($(spbm[index]).val());
                pid_arr.push($(productid[index]).val());
            });

            //含税标志
            var ytax = 0;
            if (window.hasTax) {
                ytax = 1;
            }

            var params = {
                companyid: companyid,
                khmc: khmc,
                khsh: khsh,
                dzdh: dzdh,
                dh: dh,
                khyh: khyh,
                yhzh: yhzh,
                bz: bz,
                skr: skr,
                fhr: fhr,
                kpr: kpr,
                hasTax: ytax,
                spmc: sp_arr,
                spbm : bm_arr,
                productid: pid_arr,
                ggxh: gg_arr,
                jldw: jldw_arr,
                sl: sl_arr,
                dj: dj_arr,
                je: je_arr,
                slv: slv_arr,
                se: se_arr
            }
            $(this).attr("disabled", "disabled");
            var jparam = JSON.stringify(params);

            $.post(url, { json: jparam }, function (res) {
                if (res.code == 0) {
                    layer.msg('开具成功', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        window.location.reload();
                    });

                } else {
                    layer.msg(res.msg);
                }

            } , 'json');


            layer.close(index);
        });

    });

    //删除按钮
    $("#delDetail").click(function (e) {
        var c = $("input:checkbox:checked");
        for (var i = 0; i < c.length; i++) {
            $("#" + $(c[i]).val()).remove();
        }
    });
})